//
//  ViewController.m
//  ZJPhotoBrowserTest
//
//  Created by ZJ on 2018/5/29.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ViewController.h"
#import "ZJPhotoBrowserController.h"

@interface ViewController () <ZJPhotoBrowserControllerDelegate>

@property (nonatomic, strong) NSMutableArray *photos;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self prepareData];
    [self test];
    [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];
}

- (void)test {
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    btn.center = self.view.center;
    btn.backgroundColor = [UIColor redColor];
    
    [btn setTitle:@"Click me" forState:UIControlStateNormal];
    [btn setTitle:@"Click me" forState:UIControlStateHighlighted];
    
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    
//    ZJPhoto *photo = self.photos.firstObject;
//    UIImageView *imgView = [[UIImageView alloc] initWithImage:photo.underlyingImage];
//    imgView.center = self.view.center;
//    [self.view addSubview:imgView];
}

- (NSMutableArray *)photos {
    if (_photos == nil) {
        _photos = [NSMutableArray array];
    }
    
    return _photos;
}

- (void)prepareData {
    UIImage *img1 = [UIImage imageNamed:@"image1"];
    [self.photos addObject:[ZJPhoto photoWithImage:img1]];
    
    UIImage *img2 = [UIImage imageNamed:@"image2"];
    [self.photos addObject:[ZJPhoto photoWithImage:img2]];
    
    UIImage *img3 = [UIImage imageNamed:@"timg"];
    [self.photos addObject:[ZJPhoto photoWithImage:img3]];
    
//    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"20170829_173401.mov" ofType:nil]];
//    [self.photos addObject:[ZJPhoto videoWithURL:url]];
}

- (void)btnClick {
//    ZJGridViewController *vc = [[ZJGridViewController alloc] initWithFrame:self.view.bounds];
//
//    [self addChildViewController:vc];
//    [self.view addSubview:vc.view];
//
//    [vc.view layoutIfNeeded];
//
//    [vc willMoveToParentViewController:self];
//    [UIView animateWithDuration:0 animations:^(void) {
//        vc.view.frame = self.view.bounds;
//    } completion:^(BOOL finished) {
//        [vc didMoveToParentViewController:self];
//    }];
    
    ZJPhotoBrowserController *vc = [[ZJPhotoBrowserController alloc] initWithDelegate:self];
//    vc.enableGrid = YES;
    vc.startOnGrid = YES;
    vc.backgroundColor = [UIColor whiteColor];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSUInteger)numberOfPhotosInPhotoBrowser:(ZJPhotoBrowserController *)photoBrowser {
    return self.photos.count;
}

- (id<ZJPhotoProtocol>)photoBrowser:(ZJPhotoBrowserController *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        return [self.photos objectAtIndex:index];
    }
    
    return nil;
}

- (id<ZJPhotoProtocol>)photoBrowser:(ZJPhotoBrowserController *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        return [self.photos objectAtIndex:index];
    }
    
    return nil;
}

- (BOOL)photoBrowser:(ZJPhotoBrowserController *)photoBrowser deletePhotoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        [self.photos removeObjectAtIndex:index];
        return YES;
    }
    
    return NO;
}

@end
